package com.dk.service.impl;

import com.dk.mapper.RoleMenuMapper;
import com.dk.mapper.TRoleMapper;
import com.dk.pojo.RoleMenu;
import com.dk.pojo.RoleMenuExample;
import com.dk.pojo.TRole;
import com.dk.pojo.TRoleExample;
import com.dk.service.TRoleServiceI;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service //dubbo生产者暴露接口 alibaba包
public class TRoleServiceImpl implements TRoleServiceI {

    //角色表
    @Autowired
    private TRoleMapper tRoleMapper;

    //权限角色中间表
    @Autowired
        private RoleMenuMapper roleMenuMapper;

    @Override
    public List<TRole> getCarAllInfo() {
        List<TRole> list = tRoleMapper.getCarAllInfo();
        return list;
    }


    //事物管理(注解)
    @Transactional
    @Override
    public void saveById(String roleName, Integer[] nodeIds) {

        TRole tRole = new TRole();
        tRole.setRoleName(roleName);
        //增加返回ID
        int i =  tRoleMapper.saveById(tRole);

        List<RoleMenu> roleMenus = new ArrayList<>();
        for(Integer menuId : nodeIds){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(tRole.getRoleId());
            roleMenu.setMenuId(menuId);
            roleMenus.add(roleMenu);
        }
        int j = roleMenuMapper.insertBatch(roleMenus);
    }

    @Override
    public TRole getUpdate(Integer roleId) {
        return tRoleMapper.selectByPrimaryKey(roleId);
    }


    //事物管理(注解)
    @Transactional
    @Override
    public void updateById(TRole tRole, Integer[] nodeIds) {
        //根据id修改角色
        tRoleMapper.updateByPrimaryKeySelective(tRole);
        //根据角色id条件删除中间表
        RoleMenuExample example = new RoleMenuExample();
        RoleMenuExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(tRole.getRoleId());
        roleMenuMapper.deleteByExample(example);

        //在增加新数据
        List<RoleMenu> roleMenus = new ArrayList<>();
        for(Integer menuId : nodeIds){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(tRole.getRoleId());
            roleMenu.setMenuId(menuId);

            roleMenus.add(roleMenu);
        }
        int j = roleMenuMapper.insertBatch(roleMenus);
    }

    @Override
    public void deleteById(Integer roleId) {
        //根据角色id删除
        tRoleMapper.deleteByPrimaryKey(roleId);
        //根据角色id 删除角色权限表
        //根据角色id条件删除中间表
        RoleMenuExample example = new RoleMenuExample();
        RoleMenuExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        roleMenuMapper.deleteByExample(example);

    }

    @Override
    public List<TRole> getInfoServer() {
        return tRoleMapper.selectByExample(new TRoleExample());
    }

    @Override
    public List<TRole> getUpdateInfo(Integer pid) {
        return tRoleMapper.getUpdateInfo(pid);
    }

}
